package cn.dugcrs.org.controller;

import cn.dugcrs.basic.utils.AjaxResult;
import cn.dugcrs.basic.utils.QueryResult;
import cn.dugcrs.org.domain.Department;
import cn.dugcrs.org.query.DepartmentQuery;
import cn.dugcrs.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /*
    * @Title: findDepartmentById
    * @Description: 查询单条数据
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/22 13:13
    * @Parameters: * @param id
    * @Return cn.dugcrs.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department findDepartmentById(@ApiParam(name="ID属性",required = true)@PathVariable("id") Long id){
        Department departmentById = departmentService.loadById(id);
        return departmentById;
    }

    /**
    * @Title: findDeptAll
    * @Description: 查询所有
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/24 21:33
    * @Parameters: * @param
    * @Return java.util.List<cn.dugcrs.org.domain.Department>
    */
    @GetMapping
    public List<Department> findDeptAll(){
        List<Department> findDeptAll = departmentService.loadAll();
        return findDeptAll;
    }


    /*
    * @Title: findDepartments
    * @Description: 查询符合高级查询条件及分页的全部数据
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/22 13:14
    * @Parameters: * @param departmentQuery
    * @Return cn.dugcrs.basic.utils.QueryResult
    */
    @PostMapping
    public QueryResult findDepartments(@RequestBody DepartmentQuery departmentQuery){
        QueryResult departments = departmentService.queryPage(departmentQuery);
        return departments;
    }

    /*
    * @Title: findDepartments
    * @Description: 根据id删除
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/22 13:20
    * @Parameters: * @param id
    * @Return cn.dugcrs.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    public AjaxResult delDepartmentById(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.success();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: addOrUpd
    * @Description: 添加或修改
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/22 13:55
    * @Parameters: * @param department
    * @Return cn.dugcrs.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
    * @Title: batchDelDept
    * @Description: 批量删除
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/24 18:06
    * @Parameters: * @param ids
    * @Return cn.dugcrs.basic.utils.AjaxResult
    */
    @PatchMapping
    public AjaxResult batchDelDept(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.success();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
    * @Title: findDepartmentTree
    * @Description: 查询部门树数据
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/24 18:38
    * @Parameters: * @param
    * @Return java.util.List<cn.dugcrs.org.domain.Department>
    */
    @GetMapping("/findDepartmentTree")
    public List<Department> findDepartmentTree(){
        return departmentService.findDepartmentTree();
    }



}
